package com.grass.cropTradingCenter.utils;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SafeUtils {

    /**
     * 密码加密的盐值
     */
    public static final String PASSWORD_SALT = "UNSAVORY";


    /**
     * 双重md5加密
     * @param password
     * @return
     */
    public static String getSafePassword(String password){
        return getMD5String(getMD5String(password + PASSWORD_SALT) + PASSWORD_SALT);
    }

    /**
     * 获取md5串
     * @param str
     * @return
     */
    public static String getMD5String(String str){
        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest = md5.digest(str.getBytes(StandardCharsets.UTF_8));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        assert digest != null;
        return new BigInteger(1, digest).toString(16);
    }


}
